home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / matrixc.zip / 3DEX4.C < prev    next >
C/C++ Source or Header  |  1990-05-10  |  2KB  |  116 lines

  1. /* Example of use of 3D routines to rotate cylinder  on X,Y,Z axes */
  2. /* Make using 
  3.    make make3dex.mak */
  4. /* Written by Nigel Salt */
  5.  
  6. #include <3d.h>
  7. #include <graph.h>
  8. #include <math.h>
  9.  
  10. double dmatA[4][4];
  11. matrix matA={4,4,&dmatA[0][0]};
  12.  
  13. double dmatB[4][4];
  14. matrix matB={4,4,&dmatB[0][0]};
  15.  
  16. double dmatC[4][4];
  17. matrix matC={4,4,&dmatC[0][0]};
  18.  
  19. double dmatD[4][4];
  20. matrix matD={4,4,&dmatD[0][0]};
  21.  
  22. double dmatE[4][4];
  23. matrix matE={4,4,&dmatE[0][0]};
  24.  
  25. double dmatF[4][4];
  26. matrix matF={4,4,&dmatF[0][0]};
  27.  
  28. double dmatG[4][4];
  29. matrix matG={4,4,&dmatG[0][0]};
  30.  
  31. double dmatH[4][4];
  32. matrix matH={4,4,&dmatH[0][0]};
  33.  
  34. double dmatI[4][4];
  35. matrix matI={4,4,&dmatI[0][0]};
  36.  
  37. double dmatJ[4][4];
  38. matrix matJ={4,4,&dmatJ[0][0]};
  39.  
  40. /* OBJECTS */
  41. #define CYLBAS 20
  42. double cylp[CYLBAS*2][3];
  43. int cyll[CYLBAS*3][2];
  44. object cyl=
  45. {
  46.   CYLBAS*2,CYLBAS*3,&cylp[0][0],&cyll[0][0]
  47. };
  48.  
  49. double cylp2[CYLBAS*2][3];
  50. int cyll2[CYLBAS*3][2];
  51. object cyl2=
  52. {
  53.   CYLBAS*2,CYLBAS*3,&cylp2[0][0],&cyll2[0][0]
  54. };
  55.  
  56. main()
  57. {
  58.   int i,j,k;
  59.   char buff[80];
  60.   cyldef(CYLBAS,&cyl);
  61.   init3d();
  62.   scale3(&matA,50,50,50);
  63.   objtran(&cyl,&matA);
  64.  
  65.   for (j=0;j<3;j++)
  66.     {
  67.     rot3(&matA,pi/16,j);
  68.     _settextposition(30,0);
  69.     sprintf(buff,"%c ROT",'X'+j);
  70.     _outtext(buff);
  71.     for (i=0;i<50;i++)
  72.       {
  73.       _setcolor(i%15+1);
  74.       objdraw(&cyl);
  75.       objcop(&cyl,&cyl2);
  76.       objtran(&cyl,&matA);
  77.       _setcolor(0);
  78.       objdraw(&cyl2);
  79.       }
  80.     }
  81.   _setvideomode(_DEFAULTMODE);
  82. }
  83.  
  84. wait()
  85. {
  86.  _settextposition(30,70);
  87.  _outtext("PRESS KEY");
  88.  getch();
  89.  _settextposition(30,70);
  90.  _outtext("         ");
  91. }
  92. cyldef(max,o)
  93. int max;
  94. objectptr o;
  95. {
  96.   int i;
  97.   double anginc,ang;
  98.   ang=-pi;
  99.   anginc=pi/max*2;
  100.   for (i=0;i<max;i++)
  101.     {
  102.     *(o->pdat+(i+max)*3+0)=cos(ang);
  103.     *(o->pdat+i*3+0)=cos(ang);
  104.     *(o->pdat+i*3+1)=1;
  105.     *(o->pdat+(i+max)*3+1)=-1;
  106.     *(o->pdat+(i+max)*3+2)=*(o->pdat+i*3+2)=sin(ang);
  107.     ang+=anginc;
  108.     *(o->ldat+i*2)=i;
  109.     *(o->ldat+i*2+1)=(i+1)%max;
  110.     *(o->ldat+(i+max)*2)=i+max;
  111.     *(o->ldat+(i+max)*2+1)=(i+1)%max+max;
  112.     *(o->ldat+(i+2*max)*2)=i;
  113.     *(o->ldat+(i+2*max)*2+1)=(i+max);
  114.     }
  115. }
  116.